﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MT.CustomWindows
{
    /// <summary>
    /// LoadingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingWindow : Window
    {
        public LoadingWindow()
        {
            InitializeComponent();
        }

        private Action progressAction;
        private LoadingWindow(Action act, string title, string mes)
        {
            InitializeComponent();

            TextBlock_Title.Text = title;
            Label_PrgressInfo.Content = mes;

            progressAction = act;
        }

        private void WinLoading_Loaded(object sender, RoutedEventArgs e)
        {
           // progressAction.BeginInvoke(OnComplate, null);
        }

        public static void Loading(Action act, string title, string mes = "请稍后...")
        {
            LoadingWindow win = new LoadingWindow(act, title, mes);
            var task = Task.Factory.StartNew(() => { win.progressAction?.Invoke(); });
            Task.Factory.StartNew(() =>
            {
                Task.WaitAll(task);
                win.Dispatcher.Invoke(new Action(() =>
                {
                    win.Close();
                }));
            });

            win.ShowDialog();
        }
    }
}
